<?php
require_once ("../src/db/connectdatabase.php");

function getCards($bankid) {
	$con = new ConnectDB();
	
	$tbl = "card";
	$fn = array('idcard', 'cardname', 'imagecard', 'bankid', 'bankname', 'banklogo', 'datecreate');
	$condetion = array('isuse' => true);
	$flike = array('bankid' => $bankid);
	return $con -> getvalueall($tbl, $fn, $condetion, $flike, 'datecreate');
}

function int_to_date($int)
{
    $time  = date("d/m/Y", $int);
    return $time;
}

$bankid = "";
if(isset($_GET['selectbank'])) {
	$bankid = $_GET['selectbank'];
	if($bankid == 'all') {
		$bankid = "";
	}
}

$arrcards = getCards($bankid);
?>
<style>
table {
	width: 900px;
	max-width: 900px;
}

table td {
	margin-top: 2px;
	border: 1px solid;
	border-radius: 2px 2px 2px 2px;
}

table td img {
	cursor: pointer;
}

table tr:hover td {
	background: #FFC;
}

.title {
	text-align: center;
	background-color: #0088cc;
	min-height: 40px;
	-moz-border-radius: 8px 8px 0px 0px;
	-webkit-border-radius: 8px 8px 0px 0px;
	border-radius: 8px 8px 0px 0px;
	color: #FFF;
}

.stylenameproduct {
	padding-left: 5px;
	padding-right: 5px;
	width: 200px;
	max-width: 200px;
}

.stylelogobank {
	width: 100px;
	text-align: center;
	max-width: 100px;
}

.styleacction {
	width: 80px;
	text-align: center;
	max-width: 80px;
}

.styledatecreateth {
	width: 100px;
	max-width: 100px;
	text-align: center;
}

.nameformtd {
	background: url(../images/bg-login.png);
	-moz-border-radius: 2px 2px 2px 2px;
	-webkit-border-radius: 2px 2px 2px 2px;
	border-radius: 2px 2px 2px 2px;
	text-align: center;
	padding:2px;
}

select {
	width: 170px;
	height: 30px;
	padding: 5px;
	margin-left: 5px;
	margin-right: 5px;
	margin-bottom: 5px;
}

.stylestt {
	width: 50px;
}
</style>
<h3>Sản Phảm Thẻ Tín Dụng Có Sẳn</h3>
<div class="searchforlaisuat">
<form action="admin.php" method="get">
	<input type="hidden" value="quanlythetindung" name="content" />
	<h4 style="color:#03F; margin-bottom: -10px;">Lọc Theo:</h4><br>
	<label for="selectbank">Ngân Hàng</label>
	<select name="selectbank" class="selectbank">
    	<option value="all">----Chọn Ngân Hàng----</option>
    </select>
    <input type="submit" name="submit" value="Lọc" style="border-radius: 5px; padding:5px; width:50px; background:#03F; color:#FFF; font-weight:bold;" />
</form>
</div><hr /><br />
<div>
	<table>
    	<tr>
        	<th class="stylestt title">STT</th>
        	<th class="stylenameproduct title">Tên Thẻ</th>
            <th class="stylelogobank title">Hình Ảnh</th>
            <th class="stylelogobank title">Ngân Hàng</th>
            <th class="styledatecreateth title">Ngày Cập Nhật</th>
            <th class="styleacction title">Chức Năng</th>
        </tr>
        <?php
		if(count($arrcards) > 0) {
			$i = 0;
			foreach($arrcards as $entry) {
				$i++;
		?>
        <tr>
        	<td class="nameformtd" style="text-align:center;"><?php echo($i) ?></td>
        	<td class="nameformtd" style="text-align:left;"><?php echo($entry[1]) ?></td>
            <td class="nameformtd"><img src="../<?php echo($entry[2]) ?>" title="<?php echo($entry[1]) ?>" width="100px" /></td>
            <td class="nameformtd"><img src="../<?php echo($entry[5]) ?>" title="<?php echo($entry[4]) ?> (<?php echo($entry[3]) ?>)"  width="100px" /></td>
            <td class="nameformtd"><?php echo(int_to_date($entry[6])) ?></td>
            <td class="nameformtd"><img class="class-editcard-link" src="../images/icon-edit.gif" title="Chỉnh Sửa Ưu Đãi <?php echo($entry[1]) ?>" idedit="<?php echo($entry[0]) ?>" />&nbsp; | &nbsp;<img class="class-deletecard-link" src="../images/icon-delete.gif" title="Xóa Ưu Đãi <?php echo($entry[1]) ?>" idedit="<?php echo($entry[0]) ?>" /></td>
        </tr>
        <?php
			}
		} else {
		?>
        <tr><th colspan="5">Chưa có thẻ tín dụng có sẳn nào tồn tại</th></tr>
        <?php
		}
		?>
    </table>
</div>
<div><a href="admin.php?content=quanlythetindung&&as=dangttdcosan"><img src="../images/icon-add.jpg" title="Thêm Thẻ Tín Dụng Có Sẳn" />Thêm Thẻ Tín Dụng Có Sẳn</a></div>
<script>
loadbank();
function loadbank() {
	$.ajax({
		url: "../src/ajax/ajaxbank.php",
		type: "post",
		data: {act:'getbanks'},
		dataType:"json",
		success: function(response) {
			html = '';
			$.each(response, function(entryIndex, entry){
				html += '<option value="' + entry[0] + '">' + entry[1] + '(' + entry[0] + ')</option>';
			});
			$('.selectbank').append(html);
			$(".selectbank option[value='<?php echo($bankid) ?>']").attr('selected', 'selected');
		}
	});
}

$(document).on('click', '.class-editcard-link', function(e) {
		elem = $(this)
		var id = elem.attr('idedit');
		window.location= 'admin.php?content=quanlythetindung&&as=editttdcosan&&id=' + id;
});

$(document).on('click', '.class-deletecard-link', function(e) {
	elem = $(this)
	var id = elem.attr('idedit');
	if(confirm("Bạn có muốn xóa?")) {
		$.ajax({
			url: "../src/ajax/ajaxcardcosan.php",
			type: "post",
			data: {act:'del', idre: id},
			dataType:"json",
			success: function(data) {
				if(data == true) {
					elem.parent().parent().fadeOut(300, function(){elem.parent().parent().remove();});
				} else {
					alert("Cập nhật dữ liệu không thất bại");
				}
			}
		});
	}
});
</script>